package com.example.wan1.view.login;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;

import androidx.arch.core.executor.ArchTaskExecutor;

import com.example.wan1.app.MyApplication;
import com.example.wan1.bus.RxBus;
import com.example.wan1.data.BaseObserver;
import com.example.wan1.data.RetrofitUtil;
import com.example.wan1.data.RxSchedulers;
import com.example.wan1.event.LoginEvent;
import com.example.wan1.model.CoinBean;
import com.example.wan1.model.UserBean;

import org.greenrobot.greendao.query.QueryBuilder;

/***
 * @Author: zxd
 * @CreateDate: 2020/11/26 9:48
 * @Description: 用户管理类
 * 静态内部类的形式生成单例
 * https://blog.csdn.net/mnb65482/article/details/80458571
 */
public class UserManager {

    private static class Holder {
        public static final UserManager INSTANCE = new UserManager();
    }

    public static UserManager getInstance() {
        return Holder.INSTANCE;
    }

    public void save(UserBean userBean) {
        MyApplication.getDaoSession().getUserBeanDao().insertOrReplace(userBean);
    }

    public void delete() {
        MyApplication.getDaoSession().getUserBeanDao().deleteAll();
    }

    public UserBean getUser() {
        QueryBuilder<UserBean> userBeanQueryBuilder = MyApplication.getDaoSession().getUserBeanDao().queryBuilder();
        if (userBeanQueryBuilder.list().size() > 0) {
            UserBean userBean = userBeanQueryBuilder.list().get(0);
            Log.i("aaa", "getUser: " + userBean.getCoinCount() + userBean.getUsername());
            return userBeanQueryBuilder.list().get(0);
        }
        return null;
    }

    /***
     * 是否登陆
     */
    public boolean isLogin() {
        if (getUser() == null)
            return false;
        else
            return true;
    }

    /***
     * 是在已经登陆的前提下进行刷新的
     */
    @SuppressLint("RestrictedApi")
    public void refresh() {
        if (!isLogin()) return;
        ArchTaskExecutor.getIOThreadExecutor().execute(new Runnable() {
            @Override
            public void run() {
                RetrofitUtil.getInstance().getCoin()
                        .compose(RxSchedulers.observableIO2Main())
                        .subscribe(new BaseObserver<CoinBean>() {
                            @Override
                            public void onSuccess(CoinBean coin) {
                                if (coin != null) {
                                    UserBean user = getUser();
                                    if (user != null) {
                                        user.setRank(coin.getRank());
                                        user.setCoinCount(coin.getCoinCount());
                                        user.setLevel(coin.getLevel());
                                        UserManager.getInstance().save(user);
                                        RxBus.getDefault().post(new LoginEvent("1"));
                                    }
                                }
                            }

                            @Override
                            public void onFailure(Throwable e, String errorMsg) {
                                //JumpTool.goLogin(context);
                                //isLogin = false;
                            }
                        });
            }
        });
    }
}
